package br.ufpe.cin.mac.middleware.autonomic;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class KnowledgeBase {

	private List<Transition> transitions;

	public KnowledgeBase() {
		this.transitions = new ArrayList<Transition>();
	}

	/**
	 * @return the transitions
	 */
	public List<Transition> getTransitions() {
		return transitions;
	}

	/**
	 * @param transitions
	 *            the transitions to set
	 */
	public void setTransitions(List<Transition> transitions) {
		this.transitions = transitions;
	}

	/**
	 * @param index
	 * @param element
	 * @see java.util.List#add(int, java.lang.Object)
	 */
	public void addTransition(int index, Transition element) {
		transitions.add(index, element);
	}

	/**
	 * @param e
	 * @return
	 * @see java.util.List#add(java.lang.Object)
	 */
	public boolean addTransition(Transition e) {
		return transitions.add(e);
	}

	/**
	 * 
	 * @see java.util.List#clear()
	 */
	public void clearTransitions() {
		transitions.clear();
	}

	/**
	 * @param index
	 * @return
	 * @see java.util.List#remove(int)
	 */
	public Transition remove(int index) {
		return transitions.remove(index);
	}

	/**
	 * @param o
	 * @return
	 * @see java.util.List#remove(java.lang.Object)
	 */
	public boolean remove(Object o) {
		return transitions.remove(o);
	}

	/**
	 * Filter Transitions Per BeforeConfig Minute
	 * 
	 * @param actualConfig
	 * @return
	 */
	public List<Transition> filterMinute(Config actualConfig) {
		List<Transition> transTemp = new ArrayList<Transition>();
		Calendar c = actualConfig.getCalendar();

		for (Transition tempT : transitions) {
			Calendar cTemp = tempT.getBeforeConfig().getCalendar();
			if (cTemp.get(Calendar.MINUTE) == c.get(Calendar.MINUTE)) {
				transTemp.add(tempT);
			}
		}
		return transTemp;
	}

	/**
	 * Filter Transitions Per BeforeConfig Hour
	 * 
	 * @param actualConfig
	 * @return
	 */
	public List<Transition> filterHour(Config actualConfig) {
		List<Transition> transTemp = new ArrayList<Transition>();
		Calendar c = actualConfig.getCalendar();

		for (Transition tempT : transitions) {
			Calendar cTemp = tempT.getBeforeConfig().getCalendar();
			if (cTemp.get(Calendar.HOUR_OF_DAY) == c.get(Calendar.HOUR_OF_DAY)) {
				transTemp.add(tempT);
			}
		}
		return transTemp;
	}

	/**
	 * Filter Transitions Per BeforeConfig Day
	 * 
	 * @param actualConfig
	 * @return
	 */
	public List<Transition> filterDay(Config actualConfig) {
		List<Transition> transTemp = new ArrayList<Transition>();
		Calendar c = actualConfig.getCalendar();

		for (Transition tempT : transitions) {
			Calendar cTemp = tempT.getBeforeConfig().getCalendar();
			if (cTemp.get(Calendar.DAY_OF_WEEK) == c.get(Calendar.DAY_OF_WEEK)) {
				transTemp.add(tempT);
			}
		}
		return transTemp;
	}

	/**
	 * Filter Transitions Per BeforeConfig Month
	 * 
	 * @param t
	 * @return
	 */
	public List<Transition> filterMonth(Config actualConfig) {
		List<Transition> transTemp = new ArrayList<Transition>();
		Calendar c = actualConfig.getCalendar();

		for (Transition tempT : transitions) {
			Calendar cTemp = tempT.getBeforeConfig().getCalendar();
			if (cTemp.get(Calendar.MONTH) == c.get(Calendar.MONTH)) {
				transTemp.add(tempT);
			}
		}
		return transTemp;
	}

	/**
	 * Filter Transitions Per BeforeConfig Month
	 * 
	 * @param t
	 * @return
	 */
	public List<Transition> filterInterceptors(Config actualConfig) {
		List<Transition> transTemp = new ArrayList<Transition>();
		Set<String> setInterceptors = new HashSet<String>(actualConfig
				.getInterceptors());

		for (Transition tempT : transitions) {
			Set<String> setTemp = new HashSet<String>(tempT.getBeforeConfig()
					.getInterceptors());
			if (setInterceptors.equals(setTemp)) {
				transTemp.add(tempT);
			}
		}
		return transTemp;
	}

	/**
	 * 
	 * @param actualConfig
	 * @return
	 */
	public List<Transition> filterRule(String rule) {
		List<Transition> transTemp = new ArrayList<Transition>();

		for (Transition tempT : transitions) {
			if (tempT.getRule().equals(rule))
				transTemp.add(tempT);
		}
		return transTemp;
	}
}
